﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Parsowanie;
using Portfel.IS;
using Portfel.ViewModels;
using Portfel.Web;
using Portfel.enums;
using Telerik.Windows.Controls;
using Telerik.Windows.Data;

namespace Portfel
{
    public partial class Gielda : PhoneApplicationPage
    {
		private PivotBlocker pivotBlocker;

		public Gielda()
        {
            InitializeComponent();
			this.pivotBlocker = new PivotBlocker();
          //  this.DataBoundListBox1.ItemsSource = IsolatedStorageHelper.ReadFromXml<List<SecurityIndice>>(IsolatedStorageHelper.AkcjeFile);
        }

		private void RadDataBoundListBox_IsCheckModeActiveChanged(object sender, Telerik.Windows.Controls.IsCheckModeActiveChangedEventArgs e)
        {
            var listBox = sender as RadDataBoundListBox;
            if (listBox == null)
            {
                return;
            }
			if (listBox.IsCheckModeActive)
            {
                this.pivotBlocker.Start(this.MainPivot);
            }
            else
            {
                this.pivotBlocker.End();
            }
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            if (this.MainPivot.SelectedIndex == 0)
            {
                this.AkcjeList.IsCheckModeActive ^= true;
            }
            else
            {
                this.ObligacjeList.IsCheckModeActive ^= true;
            }
        }
        private void GroupPickerItemTap(object sender, GroupPickerItemTapEventArgs e)
        {
            var jumplist = (RadJumpList) sender;
            foreach (DataGroup group in jumplist.Groups)
            {
                if (object.Equals(e.DataItem, group.Key))
                {
                    e.DataItemToNavigate = group;
                    return;
                }
            }
        }

        private void ApplicationBarIconRefresh_OnClick(object sender, EventArgs e)
        {
            switch (MainPivot.SelectedIndex)
            {
                case (int)PivotIndexes.Akcje:
                    WebData.GetAkcje();
                  break;
                case (int)PivotIndexes.Obligacje:
                    WebData.GetObligacje();
                    break;
            }
        }

        private void ApplicationBarIconBuy_OnClick(object sender, EventArgs e)
        {
            var items = ObligacjeList.CheckedItems;
            var obli = items.OfType<DataItemViewModel>().ToList();

          items = AkcjeList.CheckedItems;
            var akcje = items.OfType<DataItemViewModel>().ToList();
            var para = new Tuple<List<DataItemViewModel>, List<DataItemViewModel>>(akcje, obli);
            // var bought = items.OfType<DataSourceItem>().Select(sourceItem => (DataItemViewModel) sourceItem.Value).ToList();
            PageNavigator.Navigator.Instance.Navigate<MojePapiery>(para);
        }
    }
}
